<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            padding: 100px;
        }

        textarea {
            width: 200px;
            height: 100px;
            border: 1px solid pink;
            outline: none;
            resize: none;
        }

        ul {
            margin-top: 50px;
        }

        li {
            width: 300px;
            padding: 5px;
            background-color: rgb(245, 209, 243);
            color: red;
            font-size: 14px;
            margin: 15px 0;
        }
        li a{
            float: right;
        }
    </style>
</head>
<body>
<textarea name="" id=""></textarea>
<button>发布</button>
<ul>

</ul>

<script>
    var textarea = document.querySelector('textarea');
    var btn = document.querySelector('button');
    var ul = btn.nextElementSibling;
    btn.onclick = function () {
        var content = textarea.value;
        if (content == '') {
            alert('您没有输入信息')
        } else {
            console.log(content);
            //1.创建元素
            var li = document.createElement('li');
            // var delBtn = document.createElement('Button');
            li.innerHTML = content+"<a href='javascript:;'>删除</a>";
            // delBtn.innerHTML = '删除';
            //2.添加元素
            // ul.appendChild(li);
            ul.insertBefore(li, ul.children[0]);
            //3.删除元素 删除的是当前链接的li 他的父亲
            var as = document.querySelectorAll('a');
            for (var i = 0;i<as.length;i++){
                as[i].onclick = function () {
                    //node.removeChild(Child);删除的是li 当前的a所在的li this.parentNode;
                    ul.removeChild(this.parentNode);
                }
            }

        }

    }
</script>
</body>
</html>